package com.android.myapplication;


import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.CookieManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.android.myapplication.event.Event_ChangeUrl;
import com.android.myapplication.util.SPUtil;

import org.greenrobot.eventbus.EventBus;

public class SettingActivity extends FragmentActivity {
    TextView tv_save;   //保存按钮
    EditText et_url;
    ImageView iv_back;

    Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        CookieManager.getInstance().removeAllCookie();   //清楚全部缓存
        setContentView(R.layout.activity_setting);
        initView();
    }

    //初始化控件
    private void initView() {
        tv_save = findViewById(R.id.tv_save);
        et_url = findViewById(R.id.et_url);
        iv_back = findViewById(R.id.iv_back);
        tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击保存
                String url = et_url.getText().toString();
                if (TextUtils.isEmpty(url)) {
                    Toast.makeText(mContext, "请输入链接地址", Toast.LENGTH_LONG).show();
                    return;
                }

                if (!url.startsWith("http:") && !url.startsWith("https:")){
                    url = "http://"+ url;
                }
                SPUtil.putValue(mContext, "HOST", url);
                Toast.makeText(mContext, "保存成功", Toast.LENGTH_LONG).show();
                EventBus.getDefault().post(new Event_ChangeUrl());   //发通知 告诉主页 地址变了
                finish();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}
